﻿using System.Collections.Generic;
using System.Linq;
using Crystal.Common.Model;
using System.Data;
using System.Linq.Expressions;
using System;
using System.IO;
using System.ComponentModel;
using Crystal.CLL;


namespace CrystalAdmin.BLL
{
    public class UserManager : IDisposable
    {
        #region Fields

        #endregion

        #region Constructors

        public UserManager()
        {
        }

        #endregion

        #region Methods
        public void Dispose() { }
        public BindingList<USR_Users> FindUserList(CrystalEntities context)
        {
            IEnumerable<ClientName> query1 =
                from p in context.USR_Users
                select new ClientName
                {
                    CName = p.UserName
                }; 
            string strDeletedUsers = UserStatus.DELTE.ToString();
            var query = (from c in context.USR_Users
                         where c.UserStatus!=strDeletedUsers
                         select c);
            return new BindingList<USR_Users>(query.ToList());

            
        }

        public BindingList<USR_Users> FindUserByName(CrystalEntities context, string searchtext)
        {
            string strDeletedUsers = UserStatus.DELTE.ToString();
            
            var query = (from c in context.USR_Users
                         where (c.FirstName.Contains(searchtext) || c.LastName.Contains(searchtext) || c.UserName.Contains(searchtext) || c.EMailAddress.Contains(searchtext)) && c.UserStatus!=strDeletedUsers
                         select c);
            var result = query.ToList();
            return new BindingList<USR_Users>(query.ToList());
        }

        public void AddEditUser(CrystalEntities context, USR_Users user)
        {
            if (user.UserID == 0)
            {
                context.AddToUSR_Users(user);
                context.SaveChanges();
            }
            else
            {
                var query = (from o in context.USR_Users
                             where o.UserID == user.UserID
                             select o).First();
                context.USR_Users.ApplyCurrentValues(user);
                context.SaveChanges();
            }
        }

        public void DeleteUser(CrystalEntities context, USR_Users user)
        {
            var query = (from o in context.USR_Users
                         where o.UserID == user.UserID
                         select o).First();
            context.DeleteObject(query);
            context.SaveChanges();
        }

        public List<USR_Users> CheckUserName(CrystalEntities context, string userName)
        {
            var q = from c in context.USR_Users
                    where c.UserName == userName
                    select c;
            return q.ToList();
        }

        public int CheckUserEmail(CrystalEntities context, string emailId)
        {
            var q = from c in context.USR_Users
                    where c.EMailAddress == emailId
                    select c;
            return q.Count();
        }

        #endregion
    }
}
